const { ccclass, property } = cc._decorator;

@ccclass
export default class GameControl3 extends cc.Component {

    @property(cc.Node)
    public RewardPre: cc.Node = null;   //奖励预制节点  

    public AwardNode: cc.Node;         //奖励父节
    public Pointer: cc.Node;           //指针


    public rewardCount: number = 12;                     //奖品个数      
    public avgAngle: number = 360 / this.rewardCount;   //平均角度
    private angleStep = Math.PI * 2 / this.rewardCount; //平均弧度
    private radius: number = 240;                       //半径

    private curIndex: number = 0;    //当前奖励下标
    private tagetIndex: number = 0;  //目标奖励下标
    private reawadArr: Array<Number> = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33];  //奖品名称

    onLoad() {
        this.AwardNode = cc.find("ZhuanPanRoot/BackGround/AwardNode", this.node);
        this.Pointer = cc.find("ZhuanPanRoot/BackGround/CentreNode/Pointer", this.node);
    }

    start() {
        this._InitAwardPre();
    }


    private _InitAwardPre() {
        for (let i = 0; i < this.rewardCount; i++) {
            const angle = i * this.angleStep; // 计算当前点的角度  
            const x = this.radius * Math.cos(angle); // 使用余弦计算x偏移量        
            const y = this.radius * Math.sin(angle); // 使用正弦计算y偏移量  

            let rewardPre = cc.instantiate(this.RewardPre);
            rewardPre.x = y, rewardPre.y = x;
            rewardPre.parent = this.AwardNode;
            rewardPre.active = true;

            rewardPre.getChildByName('label').getComponent(cc.Label).string = i.toString();
        }
    }


    //开始抽奖
    private isTurnIng = false;
    public BtnCallBack() {
        if (this.isTurnIng) {
            return;
        }
        this.isTurnIng = true;

        let awardIndex = Math.floor(Math.random() * this.rewardCount);
        this.tagetIndex = awardIndex;

        console.log("========>>>>", awardIndex, this.reawadArr[awardIndex]);
        let _awardAngle = this.tagetIndex * this.avgAngle * -1;
        let _angle = 6 * 360 * -1 + _awardAngle;
        cc.tween(this.Pointer)
            .to(5, { angle:  _angle}, { easing: "quadInOut"}) // quartInOut //quadInOut
            .call(() => {
                this.isTurnIng = false;
                this.Pointer.angle = _awardAngle;
            })
            .start();



    }
    // update (dt) {}
}
